package com.phamkhanh.bo;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.phamkhanh.dao.UserDAO;
import com.phamkhanh.domain.Sim;
import com.phamkhanh.domain.User;

public class UserBOImpl implements UserBO {

	private UserDAO userDAO;
	private SimBO simBO;

	@Autowired
	public UserBOImpl(UserDAO userDAO, SimBO simBO) {
		super();
		this.userDAO = userDAO;
		this.simBO = simBO;
	}

	@Transactional
	@Override
	public void save(User user, String serialSim) {
		userDAO.save(user);
		Sim sim = new Sim();
		sim.setUser(user);
		sim.setSerialSim(serialSim);
		simBO.save(sim);
	}

	@Override
	@Transactional(readOnly=true)
	public User findById(int uid) {
		return userDAO.findById(User.class, uid);
	}
	
	@Override
	@Transactional(readOnly = true)
	public User findByUserNamePassword(String userName, String password) {
		Query query = userDAO.getSession().createQuery(" from User where userName = :userName and password = :password");
		query.setParameter("userName", userName);
		query.setParameter("password", password);
		return userDAO.findOne(query);
	}
		
}
